package acwing.图.最小生成树;

import java.util.Arrays;
import java.util.Scanner;

/**
 * Prim算法求最小生成树
 *
 * @author yang
 * @date 2022/1/13
 */
public class Prim算法求最小生成树 {

    static int N = 510;
    static int n, m;
    static int g[][] = new int[N][N];
    static int[] dist = new int[N];
    static boolean[] st = new boolean[N];
    static int INF = 0x3f3f3f3f;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        m = sc.nextInt();

        for (int i = 0; i <= n; i++) {
            Arrays.fill(g[i], INF);
        }

        while (m-- > 0) {
            int a = sc.nextInt();
            int b = sc.nextInt();
            int w = sc.nextInt();
            g[a][b] = g[b][a] = Math.min(g[a][b], w);
        }
        int t = prim();
        if (t == INF) System.out.println("impossible");
        else System.out.println(t);
    }

    private static int prim() {

        Arrays.fill(dist, INF);
        int res = 0;
        for (int i = 0; i < n; i++) {
            int t = -1;
            for (int j = 1; j <= n; j++) {
                if (!st[j] && (t == -1 || dist[t] > dist[j]))
                    t = j;
            }
            if (i > 0 && dist[t] == INF) return INF;
            if (i > 0) res += dist[t];
            for (int j = 1; j <= n; j++) {
                dist[j] = Math.min(dist[j], g[t][j]);
            }
            st[t] = true;
        }
        return res;
    }
}
